﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;
using System.Data.SqlClient;

namespace Portal.CMS
{
    public class LawDocument : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "LawDocument";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Văn bản / tài liệu");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override WebPartInt[] SubWebParts
        {
            get
            {
                WebPartInt[] SubWebParts = new WebPartInt[] 
                { 
                    new AddLawDocument(),
                    new UpdateLawDocument(),
                    new LawDocumentItems(),
                    new AddLawDocumentItem(),
                    new UpdateLawDocumentItem(),
                    new UploadLawDocumentItem(),
                    new SearchLawDocumentItems()
                };
                return SubWebParts;
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string belongSite,string Keyword, int CurrentPageIndex)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                Environments.CheckWebPartAllow(WebPartId, true);


                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocumentWebPart");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ManageLawDocument", WebPartId, null, true);

                string SelectSite =
                    "<select id=drpSelectSite onchange=javascript:FilterFunc();>";
                string[] Sites = Environments.GetSites();
                for (int iIndex = 0; iIndex < Sites.Length; iIndex++)
                {
                    if (Sites[iIndex].ToLower().Equals(belongSite))
                    {
                        SelectSite += "<option selected value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                    else
                    {
                        SelectSite += "<option value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                }
                SelectSite += "</select>";

                DataTable dtLawDocument = new LawDocumentBLL().ReadingLawDocument(Keyword);
            
                int ItemPerPages = 15;
                int MaxPage = dtLawDocument.Rows.Count / ItemPerPages;
                if (dtLawDocument.Rows.Count % ItemPerPages != 0) MaxPage++;
                int StartIndex = CurrentPageIndex * ItemPerPages;
                int EndIndex = StartIndex + ItemPerPages;
                if (EndIndex > dtLawDocument.Rows.Count)
                {
                    EndIndex = dtLawDocument.Rows.Count;
                }


                string SelectPageText = "";
                if (MaxPage > 1)
                {
                    SelectPageText = ResourceBLL.GetText(WebPartId, "SelectPage", "Trang") + ": <select id=divSelectPageLawDocument onchange=javascript:JumPageLawDocument();>";
                    for (int iIndex = 0; iIndex < MaxPage; iIndex++)
                    {
                        if (CurrentPageIndex == iIndex)
                        {
                            SelectPageText += "<option selected >" + (iIndex + 1) + "</option>";
                        }
                        else
                        {
                            SelectPageText += "<option>" + (iIndex + 1) + "</option>";
                        }
                    }
                    SelectPageText += "</select>";
                }

                string RetHtml =
                    ResourceBLL.GetText(WebPartId, "SelectSite", "Site") + ":" + SelectSite + "&nbsp;" + ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa") + ": <input id=txtKeyword value=\"" + Keyword + "\">" + SelectPageText + "<input type=button value=\"" + ResourceBLL.GetText(WebPartId, "FilterButton", "Lọc") + "\" onclick=JumPageLawDocument();><input onclick=\"javascript:ClientAddLawDocument();\" type=button value=\"" + ResourceBLL.GetText(WebPartId, "TH_Add", "Thêm mới") + "\"><div id=divProcessing></div>";

                if (dtLawDocument.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=
                    "   <div class=cssTotalResultInfo>" + ResourceBLL.GetText(WebPartId, "Found", "Số bản ghi thỏa mãn điều kiện tìm") + ": " + dtLawDocument.Rows.Count.ToString("#,##0") + "</div>" +
                    "   <table>" +
                    "       <tr>" +
                    "           <th>#</th>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "TH_LawDocumentName", "Tên tài liệu") + "</th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "       </tr>";

                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        string Alias = "";
                        try
                        {
                            Alias = (string)dtLawDocument.Rows[iIndex]["Alias"];
                        }
                        catch { }
                        string LawDocumentId = (string)dtLawDocument.Rows[iIndex]["LawDocumentId"];
                        string LawDocumentName = (string)dtLawDocument.Rows[iIndex]["Subject"];
                        string DeleteUrl = "javascript:ClientDeleteLawDocument('" + LawDocumentId + "')";
                        string ItemUrl = "javascript:ClientShowItems('" + LawDocumentId + "')";
                        string AddItemUrl = "javascript:AddLawDocumentItemFromList('" + LawDocumentId + "')";
                        string PermissionUrl = "javascript:ClientPermissionLawDocument('" + LawDocumentId + "')";
                        string SyncUrl = "javascript:ClientSyncLawDocument('" + LawDocumentId + "')";

                        RetHtml +=
                        "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                        "           <td class=cssTdIndex>" + (iIndex + 1) + "</td>" +
                        "           <td class=cssTdAlias>" + Alias + "</td>" +
                        "           <td  class=cssTdLawDocumentName>" + LawDocumentName + "</td>" +
                        "           <td><a href=\"javascript:ShowClientUpdateUrlForm('" + LawDocumentId + "');\">" + ResourceBLL.GetText(WebPartId, "TH_Update", "Cập nhật") + "</a></td>" +
                        "           <td><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Delete", "Xóa") + "</a></td>" +
                        "           <td><a href=\"" + ItemUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Items", "(Văn bản)") + "</a></td>" +
                        "           <td><a href=\"" + AddItemUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_AddItemDoc", "(Thêm)") + "</a></td>" +
                        "           <td><a href=\"" + PermissionUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Permission", "(phân quyền)") + "</a></td>" +
                        "           <td><a href=\"" + SyncUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Sync", "(Sync)") + "</a></td>" +
                        "       </tr>";
                    }
                    RetHtml +=
                        "   </table>";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(LawDocument));
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "LawDocument.js") +
                    "<div id=divLawDocumentMain class=cssDivLawDocument>" + GetHtml("vie","",0) + "</div>";
            return RetHtml;
        
        }



        [AjaxPro.AjaxMethod]
        public string GetAddGroupNewUrl(string belongSite)
        {
            try
            {
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocument");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddLawDocument", WebPartId, null, true);

                return Screen.BuildDialogUrl(new ParamCls[]{
                    new ParamCls("WebPartId",new AddLawDocument().WebPartId),
                    new ParamCls("belongSite",belongSite)
            });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId,"ConfirmDeleteMsg","Bạn đã chắc chắn xóa chưa?");
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckDeleteLawDocument(string LawDocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocument");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteLawDocument", WebPartId, null, true);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteLawDocument(string LawDocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocument");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteLawDocument", WebPartId, null, true);
           

                new LawDocumentBLL().DeleteLawDocument(null, LawDocumentId);
                return ResourceBLL.GetText(WebPartId,"DeleteOkMessage","Xóa thành công");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string LawDocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocument");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateLawDocument", WebPartId, null, true);

             
                string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateLawDocument().WebPartId),
                        new ParamCls("LawDocumentId",LawDocumentId)
                    });
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetShowItemsForm(string LawDocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocumentItems");
                LawDocumentCls LawDocumentCls = new LawDocumentBLL().CreateModel(LawDocumentId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ShowItem", LawDocumentCls.LawDocumentId, LawDocumentCls.Owner, true);

                string UpdateUrl = Screen.BuildUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new LawDocumentItems().WebPartId),
                        new ParamCls("LawDocumentId",LawDocumentId)
                    });
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
            return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId,"ProcessingText","Đang đọc dữ liệu...") + "</a>";
        }

        public override bool SystemWebPart
        {
            get
            {
                return false;
            }
        }


        public override FunctionCls[] FunctionList
        {
            get
            {
                return new FunctionCls[]
                {
                    new FunctionCls("AddLawDocument",ResourceBLL.GetText(WebPartId,"AddLawDocument","Thêm mới văn bản pháp quy"),WebPartId),
                    new FunctionCls("UpdateLawDocument",ResourceBLL.GetText(WebPartId,"UpdateLawDocument","Sửa chữa văn bản pháp quy"),WebPartId),
                    new FunctionCls("DeleteLawDocument",ResourceBLL.GetText(WebPartId,"DeleteLawDocument","Xóa văn bản pháp quy"),WebPartId),
                    new FunctionCls("PermissionLawDocument",ResourceBLL.GetText(WebPartId,"PermissionLawDocument","Phân quyền truy cập"),WebPartId),
                    new FunctionCls("ShowItems",ResourceBLL.GetText(WebPartId,"ShowItems","Xem danh sách văn bản (items)"),WebPartId),
                    new FunctionCls("ManageLawDocument",ResourceBLL.GetText(WebPartId,"ManageLawDocument","Quản lý nhóm văn bản"),WebPartId),
                    new FunctionCls("DeleteItem",ResourceBLL.GetText(WebPartId,"DeleteItem","Xóa văn bản (items)"),WebPartId),
                    new FunctionCls("UpdateItem",ResourceBLL.GetText(WebPartId,"UpdateItem","Chỉnh sửa văn bản (items)"),WebPartId),
                    new FunctionCls("PermissionItem",ResourceBLL.GetText(WebPartId,"PermissionItem","Phân quyền chỉnh sửa văn bản (items)"),WebPartId),
                    new FunctionCls("DownloadItem",ResourceBLL.GetText(WebPartId,"DownloadItem","Phân quyền download file gắn kèm"),WebPartId),
                    new FunctionCls("SearchLawDocumentItems",ResourceBLL.GetText(WebPartId,"SearchLawDocumentItems","Tìm kiếm văn bản"),WebPartId),
                    new FunctionCls("NeedCheckPermissionDownload",ResourceBLL.GetText(WebPartId,"NeedCheckPermissionDownload","Cần phải kiểm tra quyền download tài liệu"),WebPartId),
                    //new FunctionCls("SearchAllLawDocumentItems",ResourceBLL.GetText(WebPartId,"SearchAllLawDocumentItems","Tìm kiếm văn bản (toàn bộ)"),WebPartId),
                };
            }
        }

        public override PermissionTypeCls[] PermissionTypes
        {
            get
            {
                return new PermissionTypeCls[]{
                    new PermissionTypeCls("LawDocumentWebPart","Văn  bản pháp quy",new string[]{"AddLawDocument","ManageLawDocument"}),
                    new PermissionTypeCls("LawDocument","Văn  bản pháp quy",new string[]{"AddItem","UpdateLawDocument","DeleteLawDocument","ShowItems","PermissionLawDocument","SearchLawDocumentItems"}),
                    new PermissionTypeCls("LawDocumentItems","Danh sách văn bản(Items)",new string[]{"DeleteItem","UpdateItem","PermissionItem","DownloadItem","NeedCheckPermissionDownload"})
                };
            }
        }

        public override PermissionTypeCls GetServicePermissionType()
        {
            return PermissionTypes[0];
        }

        [AjaxPro.AjaxMethod]
        public string GetAddLawDocumentItemUrl(string LawDocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentWebPart");
                
                LawDocumentCls LawDocumentCls = new LawDocumentBLL().CreateModel(LawDocumentId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddItem", LawDocumentCls.LawDocumentId, LawDocumentCls.Owner, true);

                string AddLawDocumentItemUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AddLawDocumentItem().WebPartId),
                        new ParamCls("LawDocumentId",LawDocumentId)
                    });
                return AddLawDocumentItemUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetShowFormSetPermissionLawDocument(string LawDocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                LawDocumentCls LawDocumentCls = new LawDocumentBLL().CreateModel(LawDocumentId);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocument");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PermissionLawDocument", LawDocumentId, LawDocumentCls.Owner, true);
                return Environment.Permission.GetPermissionUrl(WebPartId, LawDocumentId, "LawDocument");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string SyncLawDocument(string LawDocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                new LawDocumentBLL().SyncLawDocument(LawDocumentId);
                return GetText("SyncWithServerOk", "Đồng bộ với máy chủ web thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}
